# -*- coding:utf-8 -*-
from framework.interface.abstract_api import AbstractApi
from marshmallow import Schema, fields, validate
from datetime import datetime, timedelta
import pandas as pd
import numpy as np
from collections import namedtuple
from docx import Document
from docx.shared import Cm
from flask import request
import re
import subprocess

class PartOhter(AbstractApi):
    """
    接口：PartOhter
    文件名输入：part_ohter
    该接口访问地址为：http://10.10.30.247:9091/v1/part-ohter
    输入：
    功能：
    输出：
    1 模型优缺点  2. 参考文献  3. 附录 4.模型改进 5 模型二的求解与建立  6 模型三的求解与建立  7 模型四的求解与建立
    """

    class PostSchema(Schema):
        part_id = fields.Str(required=True)
        part_type_id = fields.Int(required=True)
        part_youque = fields.Str(required=True)

    def handle_post_request(self):
        part_type_id =request.get_json()['part_type_id']

        try:
            if part_type_id ==6:
                output1 = self.part_pingjia()
            elif part_type_id ==8:
                output1 = self.part_cankao()
            elif part_type_id ==9:
                output1 = self.part_fulu()
            elif part_type_id ==7:
                output1 = self.part_gaijin()
            elif part_type_id ==2:
                output1 = self.part_model_two()
            elif part_type_id ==3:
                output1 = self.part_model_three()
            elif part_type_id ==4:
                output1 = self.part_model_four()
            elif part_type_id ==5:
                output1 = self.part_linmin()
            else:
                output1 = ""
            return output1
        except Exception as e:
            return f"程序异常: {str(e)}", 500

    # 业务1
    def part_pingjia(self):
        part_id = request.get_json()['part_id']
        part_youque = request.get_json()['part_youque']
        text = {
            "textA": part_youque
        }
        doc = Document('m_pingjia.docx')
        title_model_evaluation = "模型评价"
        for paragraph in doc.paragraphs:
            if title_model_evaluation in paragraph.text:
                p = doc.add_paragraph('1.1 模型的优缺优点')
                for run in p.runs:
                    run.font.name = '黑体'
                    run.font.bold = True

                p_text = doc.add_paragraph(text['textA'])
                p_text.paragraph_format.left_indent = Cm(0.75)  # 右侧缩进

                # p_que = doc.add_paragraph('1.2 模型的缺陷')
                # for run in p_que.runs:
                #     run.font.name = '黑体'
                #     run.font.bold = True
                #
                # p_text = doc.add_paragraph(text['textB'])
                # p_text.paragraph_format.left_indent = Cm(0.75)  # 右侧缩进

                # p.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER

        doc.save(f"./static/m{part_id}.docx")
        command = [
            'unoconv',
            '-f', 'pdf',
            '-o', f"./static/m{part_id}.pdf",
            f"./static/m{part_id}.docx"
        ]
        subprocess.run(command, check=True)
        return {"msg":"true","url":f"http://106.53.97.117:7777/static/m{part_id}.pdf"}

    def part_cankao(self):
        part_id = request.get_json()['part_id']
        part_youque = request.get_json()['part_youque']
        text = {
            "textA": part_youque
        }
        doc = Document('m_cankao.docx')
        title_model_evaluation = "模型评价"
        for paragraph in doc.paragraphs:
            # if title_model_evaluation in paragraph.text:
            #     p = doc.add_paragraph('1.1 模型的优缺优点')
            #     for run in p.runs:
            #         run.font.name = '黑体'
            #         run.font.bold = True

                p_text = doc.add_paragraph(text['textA'])
                p_text.paragraph_format.left_indent = Cm(0.75)  # 右侧缩进
        doc.save(f"./static/m{part_id}.docx")
        command = [
            'unoconv',
            '-f', 'pdf',
            '-o', f"./static/m{part_id}.pdf",
            f"./static/m{part_id}.docx"
        ]
        subprocess.run(command, check=True)
        return {"msg":"true","url":f"http://106.53.97.117:7777/static/m{part_id}.pdf"}

    def part_fulu(self):
        part_id = request.get_json()['part_id']
        part_youque = request.get_json()['part_youque']
        text = {
            "textA": part_youque
        }
        doc = Document('m_fulu.docx')
        title_model_evaluation = "模型评价"
        for paragraph in doc.paragraphs:
            # if title_model_evaluation in paragraph.text:
            #     p = doc.add_paragraph('1.1 模型的优缺优点')
            #     for run in p.runs:
            #         run.font.name = '黑体'
            #         run.font.bold = True

                p_text = doc.add_paragraph(text['textA'])
                p_text.paragraph_format.left_indent = Cm(0.75)  # 右侧缩进
        doc.save(f"./static/m{part_id}.docx")
        command = [
            'unoconv',
            '-f', 'pdf',
            '-o', f"./static/m{part_id}.pdf",
            f"./static/m{part_id}.docx"
        ]
        subprocess.run(command, check=True)
        return {"msg":"true","url":f"http://106.53.97.117:7777/static/m{part_id}.pdf"}

    def part_gaijin(self):
        part_id = request.get_json()['part_id']
        part_youque = request.get_json()['part_youque']
        text = {
            "textA": part_youque
        }
        doc = Document('m_gaijin.docx')
        title_model_evaluation = "模型评价"
        for paragraph in doc.paragraphs:
            # if title_model_evaluation in paragraph.text:
            #     p = doc.add_paragraph('1.1 模型的优缺优点')
            #     for run in p.runs:
            #         run.font.name = '黑体'
            #         run.font.bold = True

                p_text = doc.add_paragraph(text['textA'])
                p_text.paragraph_format.left_indent = Cm(0.75)  # 右侧缩进
        doc.save(f"./static/m{part_id}.docx")
        command = [
            'unoconv',
            '-f', 'pdf',
            '-o', f"./static/m{part_id}.pdf",
            f"./static/m{part_id}.docx"
        ]
        subprocess.run(command, check=True)
        return {"msg":"true","url":f"http://106.53.97.117:7777/static/m{part_id}.pdf"}

    def part_model_two(self):
        part_id = request.get_json()['part_id']
        part_youque = request.get_json()['part_youque']
        text = {
            "textA": part_youque
        }
        doc = Document('m_model_two.docx')
        title_model_evaluation = "模型评价"
        for paragraph in doc.paragraphs:
            if title_model_evaluation in paragraph.text:
                p = doc.add_paragraph('1.1 模型的优缺优点')
                for run in p.runs:
                    run.font.name = '黑体'
                    run.font.bold = True

                p_text = doc.add_paragraph(text['textA'])
                p_text.paragraph_format.left_indent = Cm(0.75)  # 右侧缩进
        doc.save(f"./static/m_model_two{part_id}.docx")
        command = [
            'unoconv',
            '-f', 'pdf',
            '-o', f"./static/m_model_two{part_id}.pdf",
            f"./static/m_model_two{part_id}.docx"
        ]
        subprocess.run(command, check=True)
        return {"msg":"true","url":f"http://106.53.97.117:7777/static/m_model_two{part_id}.pdf"}

    def part_linmin(self):
        part_id = request.get_json()['part_id']
        part_youque = request.get_json()['part_youque']
        text = {
            "textA": part_youque
        }
        doc = Document('m_linmin.docx')
        title_model_evaluation = "灵敏度分析"
        for paragraph in doc.paragraphs:
            if title_model_evaluation in paragraph.text:
                # p = doc.add_paragraph('1.1 模型的优缺优点')
                # for run in p.runs:
                #     run.font.name = '黑体'
                #     run.font.bold = True

                p_text = doc.add_paragraph(text['textA'])
                p_text.paragraph_format.left_indent = Cm(0.75)  # 右侧缩进
        doc.save(f"./static/m{part_id}.docx")
        command = [
            'unoconv',
            '-f', 'pdf',
            '-o', f"./static/m{part_id}.pdf",
            f"./static/m{part_id}.docx"
        ]
        subprocess.run(command, check=True)
        return {"msg":"true","url":f"http://106.53.97.117:7777/static/m{part_id}.pdf"}

    def part_model_three(self):
        part_id = request.get_json()['part_id']
        part_youque = request.get_json()['part_youque']
        text = {
            "textA": part_youque
        }
        doc = Document('m_model_three.docx')
        title_model_evaluation = "模型评价"
        for paragraph in doc.paragraphs:
            if title_model_evaluation in paragraph.text:
                p = doc.add_paragraph('1.1 模型的优缺优点')
                for run in p.runs:
                    run.font.name = '黑体'
                    run.font.bold = True

                p_text = doc.add_paragraph(text['textA'])
                p_text.paragraph_format.left_indent = Cm(0.75)  # 右侧缩进
        doc.save(f"./static/m_model_three{part_id}.docx")
        command = [
            'unoconv',
            '-f', 'pdf',
            '-o', f"./static/m_model_three{part_id}.pdf",
            f"./static/m_model_three{part_id}.docx"
        ]
        subprocess.run(command, check=True)
        return {"msg":"true","url":f"http://106.53.97.117:7777/static/m_model_three{part_id}.pdf"}

    def part_model_four(self):
        part_id = request.get_json()['part_id']
        part_youque = request.get_json()['part_youque']
        text = {
            "textA": part_youque
        }
        doc = Document('m_model_four.docx')
        title_model_evaluation = "模型评价"
        for paragraph in doc.paragraphs:
            if title_model_evaluation in paragraph.text:
                p = doc.add_paragraph('1.1 模型的优缺优点')
                for run in p.runs:
                    run.font.name = '黑体'
                    run.font.bold = True

                p_text = doc.add_paragraph(text['textA'])
                p_text.paragraph_format.left_indent = Cm(0.75)  # 右侧缩进

        doc.save(f"./static/m_model_four{part_id}.docx")
        command = [
            'unoconv',
            '-f', 'pdf',
            '-o', f"./static/m_model_four{part_id}.pdf",
            f"./static/m_model_four{part_id}.docx"
        ]
        subprocess.run(command, check=True)
        return {"msg":"true","url":f"http://106.53.97.117:7777/static/m_model_four{part_id}.pdf"}

